<?php

/*
 * 
 * this is main.php
 * part of the dynasties2 project on www.dixieandtheninjas.net
 * 
 * created 5/31/12
 * 
 */

// check user login
session_start();


if (isset($_SESSION["user"]) && !empty($_SESSION["user"]) && $_SESSION["user"] == '1') {
//echo $_SESSION["user"];



class Setup extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url'));
            $this->load->library('form_validation','user_agent');
	}
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function index() {
            
            $time_start = microtime(true);
            
            $time_end = microtime(true);
            $time = $time_end - $time_start;
            //echo "<br>Load Time:  " . round($time, 3) . " seconds\n";
            
        } /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function setup_duchies() {
            
            $allduchies = array(
                "1" => array(1, 2, 3, 4, 6),
                "2" => array(9, 7, 8, 5),
                "3" => array(17, 19, 18, 16, 20),
                "4" => array(12, 10, 13, 21),
                "5" => array(11, 15, 14, 22),
                "6" => array(23, 24, 26, 27, 28),
                "7" => array(45, 44, 46, 51),
                "8" => array(29, 39, 40, 38, 25),
                "9" => array(32, 31, 33, 34, 30),
                "10" => array(36, 37, 35, 42),
                "11" => array(43, 50, 41, 49),
                "12" => array(47, 48, 56, 98),
                "13" => array(57, 60, 58, 59),
                "14" => array(62, 61, 70, 71),
                "15" => array(54, 55, 97, 63, 64),
                "16" => array(68, 67, 69, 85),
                "17" => array(72, 73, 74, 76),
                "18" => array(75, 80, 81, 82),
                "19" => array(77, 78, 79, 83, 84),
                "20" => array(86, 87, 88, 93, 89),
                "21" => array(95, 96, 99),
                "22" => array(90, 91, 94, 92),
                "23" => array(52, 66, 53, 100, 65),
            );
            
        }  /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        public function display_ruler_map() {
            
            
            
        } /// end public function display_ruler_map()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function generate_ruler_colors() {
            
            $query = $this->doctrine->em->createQuery("select u 
                    from ORM\Dynasties2\Characters u 
                    WHERE u.deathDate IS NULL
                        AND u.isRuler = '1'
                        ");
            
            $rulers = $query->getResult();
            
            
            $val =  sprintf("#%06X\n", mt_rand( 0, 0xFFFFFF ));
            echo "<table><tr><td bgcolor=\"$val\">lkjasdlfkjasdklfj</td></tr></table>";
            //printf( "#%06X\n", mt_rand( 0, 0xFFFFFF ));
            
            //echo $val;
            
            $rulercolors = array (
                "snow2" => "238, 233, 233",
                "papayawhip" => "255, 239, 213",
                "gainsboro" => "220, 220, 220",
                "floralwhite" => "255, 250, 240",
                "antiquewhite3" => "205, 192, 176",
                "cornsilk" => "255, 248, 220",
            );
            
            
            //print_r($rulercolors);
            
            
            
        } /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function lordsandvassals() {
            
            // pull wars: provides attacker and defender charIDs
            // pull characters (attacker and defender)
            // pull all of their lands from titles
            // that generates all of their region IDs
            //
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                $dodgerblue= ImageColorAllocate($img, 30, 144, 225);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Duchyregions u 
                            ");

                $duchies = $query->getResult();
                
                foreach ($duchies as $row) {
                    
                    $region_data = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row->getWhichRegion());
                    
                    $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                    $coordx = $region_data->getCoordX();
                    $coordy = $region_data->getCoordY();

                    ImageFillToBorder($img, $coordx, $coordy, $border, $peachpuff);
                    
                }
                
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                            WHERE u.vassalTo IS NOT NULL
                            ");

                $titles = $query->getResult();
                
                
                foreach ($titles as $row) {
                    
                        $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                        $coordx = $row -> getRegionLink()->getCoordX();
                        $coordy = $row -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $dodgerblue);
                    
                    
                }
                
                
                
                    
                   
                    
                    
                // Output image as PNG
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            
        } /// end public function lordsandvassals()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function mapwars() {
            
            // pull wars: provides attacker and defender charIDs
            // pull characters (attacker and defender)
            // pull all of their lands from titles
            // that generates all of their region IDs
            //
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                $dodgerblue= ImageColorAllocate($img, 30, 144, 225);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Wars u 
                        
                            ");

                $wars = $query->getResult();
                
                
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                
                
                $allcombatants = array();
                
                
                foreach ($wars as $row) {
                    
                    $attacker = $row -> getAttacker();
                    $defender = $row -> getDefender();
                    
                    //$allcombatants[] = $attacker;
                    //$allcombatants[] = $defender;
                    $allattackers[] = $attacker;
                    $alldefenders[] = $defender;
                    
                    
                }
                
                //foreach ($allcombatants as $row) {
                foreach ($allattackers as $row) {
                
                    
                    // get their regions from TITLES
                    $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        WHERE u.characterLink = $row
                        ");

                    $unique_rulers = $query->getResult();

                    $color1 = mt_rand(0,255);
                    $color2 = mt_rand(0,255);
                    $color3 = mt_rand(0,255);

                    foreach ($unique_rulers as $row2) {

                        //echo $row2->getRegionLink()->getId();
                        ///// new code loaded, not yet working, go back further.

                    // put display code here:

                        $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                        $coordx = $row2 -> getRegionLink()->getCoordX();
                        $coordy = $row2 -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $dodgerblue);

                    }
                    
                } /// end foreach allattackers
                foreach ($alldefenders as $row) {
                
                    
                    // get their regions from TITLES
                    $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        WHERE u.characterLink = $row
                        ");

                    $unique_rulers = $query->getResult();

                    $color1 = mt_rand(0,255);
                    $color2 = mt_rand(0,255);
                    $color3 = mt_rand(0,255);

                    foreach ($unique_rulers as $row2) {

                        //echo $row2->getRegionLink()->getId();
                        ///// new code loaded, not yet working, go back further.

                    // put display code here:

                        $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                        $coordx = $row2 -> getRegionLink()->getCoordX();
                        $coordy = $row2 -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $dodgerblue);

                    }
                    
                } /// end foreach alldefenders
               
                
                
                    
                   
                    
                    
                // Output image as PNG
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            
        } /// end public function mapwars()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function get_a_rulers_regions($data) {
            // a test helper function meant to return the region IDs that are directly controlled by a ruler
            // assume that it is passed a value, preferably the ruler's character ID
            
            //$ruler_character_id = '14281';
            
            $query = $this->doctrine->em->createQuery("
                SELECT u 
                FROM ORM\Dynasties2\Titles u
                    WHERE u.characterLink = $data
                ");
            $result = $query->getResult();
            
            $my_regions = array();
            foreach ($result as $row) {
                $my_regions[] = $row->getRegionLink()->getId();
                
            }
            return $my_regions;
            //print_r($my_regions);
            
            
        } /// end public function get_a_rulers_regions()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_display_realms() {
            
            /// this works!   displays colored map, based on rulers.  so if a ruler owns 4 counties, they're all the same color.
            
                
                $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);
                
                /////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////
                /////////////////////////////////////////////////////////
                
                 $border = ImageColorAt ($img, 51, 67);
                //$border = imagecolorallocate($img, 0, 0, 0);
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        
                            ");

                $titles = $query->getResult();
                
                $allregions = array();
                foreach ($titles as $row) {
                    
                    $owner_id = $row -> getCharacterLink()->getId();
                    
                    if (in_array($owner_id,$allregions)) {
                        //echo "<br>$owner_id In array!";
                    } else {
                        //echo "<br>Nope, $owner_id.";
                        //print_r($allregions);
                    }
                    
                    //$owner = array();
                    //$owner[] = $row -> getCharacterLink()->getId();
                    
                    //$allregions[$owner_id] = $owner;
                    $allregions[] = $owner_id;
                    
                    
                    $color1 = mt_rand(0,255);
                    $color2 = mt_rand(0,255);
                    $color3 = mt_rand(0,255);
                    //$random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                    
                    $coordx = $row -> getRegionLink()->getCoordX();
                    $coordy = $row -> getRegionLink()->getCoordY();
                    
                    //ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                    
                }
                //echo "<br>**</br>";
                //print_r(array_unique($allregions));
                $allregions_unique = array_unique($allregions);
                //echo "<br>**</br>";
                //print_r($allregions);
                
                foreach ($allregions_unique as $row) {
                    $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        WHERE u.characterLink = $row
                        ");

                $unique_rulers = $query->getResult();
                
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                
                foreach ($unique_rulers as $row2) {
                    
                    //echo $row2->getRegionLink()->getId();
                    ///// new code loaded, not yet working, go back further.
                    
                   // put display code here:
                    
                    $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                    $coordx = $row2 -> getRegionLink()->getCoordX();
                    $coordy = $row2 -> getRegionLink()->getCoordY();
                    
                    ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                    
                    
                    
                }
                    //echo "<br>";
                    
                }
                ///////////////////////////////////////////////////////
                //////////////////////////////////////////////////////
                ///////////////////////////////////////////////////////
                

                
                
                
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
                
                
            } else {
                echo "<br>No image loaded.</br>";
            }
            
        }  /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function maptest() {
            
            //echo "Test1!";
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        
                            ");

                $titles = $query->getResult();
                
                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                
                $allregions = array();
                foreach ($titles as $row) {
                    
                    $owner = $row -> getCharacterLink()->getId();
                    $allregions[] = $owner;
                    
                    
                    $color1 = mt_rand(0,255);
                    $color2 = mt_rand(0,255);
                    $color3 = mt_rand(0,255);
                    $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                    
                    $coordx = $row -> getRegionLink()->getCoordX();
                    $coordy = $row -> getRegionLink()->getCoordY();
                    
                    ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                    
                }
                
                
                

                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                //ImageFillToBorder($img, 171, 150, $border, $honeydew);
                //ImageFillToBorder($img, 204, 71, $border, $ivory3);
                //ImageFillToBorder($img, 240, 190, $border, $peachpuff);
                //ImageFillToBorder($img, 250, 140, $border, $mistyrose);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);

                // Output image as PNG
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
                
                
            } else {
                echo "<br>No image loaded.</br>";
            }
            //print_r($allregions);
            //echo "Test2!";
            /*
             * $filename="/path/to/file.jpg"; //<-- specify the image  file
                if(file_exists($filename)){ 
                header('Content-Length: '.filesize($filename])); //<-- sends filesize header
                header('Content-Type: image/jpg'); //<-- send mime-type header
                header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header
                readfile($filename); //<--reads and outputs the file onto the output buffer
                die(); //<--cleanup
                exit; //and exit
                }
             * 
             */
            
        } /// end public function maptest()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function mapclaims() {
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Claims u 
                        
                            ");

                $claims = $query->getResult();
                
                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                
                
                
                foreach ($claims as $row) {
                    
                    $owner = $row -> getCharacterLink()->getId();
                    
                    $color1 = mt_rand(0,255);
                    $color2 = mt_rand(0,255);
                    $color3 = mt_rand(0,255);
                    $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                    
                    $coordx = $row -> getTitleLink()->getRegionLink()->getCoordX();
                    $coordy = $row -> getTitleLink()->getRegionLink()->getCoordY();
                    
                    ImageFillToBorder($img, $coordx, $coordy, $border, $green);
                    
                }
                
                
                

                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                //ImageFillToBorder($img, 171, 150, $border, $honeydew);
                //ImageFillToBorder($img, 204, 71, $border, $ivory3);
                //ImageFillToBorder($img, 240, 190, $border, $peachpuff);
                //ImageFillToBorder($img, 250, 140, $border, $mistyrose);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);

                // Output image as PNG
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            /*
             * $filename="/path/to/file.jpg"; //<-- specify the image  file
                if(file_exists($filename)){ 
                header('Content-Length: '.filesize($filename])); //<-- sends filesize header
                header('Content-Type: image/jpg'); //<-- send mime-type header
                header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header
                readfile($filename); //<--reads and outputs the file onto the output buffer
                die(); //<--cleanup
                exit; //and exit
                }
             * 
             */
            
        } /// end public function mapclaims()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function maptest2() {
            
            $query = $this->doctrine->em->createQuery("select u 
                    from ORM\Dynasties2\Titles u
                        ");
            $rulers = $query->getResult();
            
            
            
            
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                //$border = imagecolorallocate($img, 0, 0, 0);
                $border = ImageColorAt ($img, 51, 67);
                $red = ImageColorAllocate($img, 255, 0, 0); 
                $green = ImageColorAllocate($img, 0, 255, 0);
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                $honeydew = ImageColorAllocate($img, 240, 255, 240);
                $ivory3 = ImageColorAllocate($img, 205, 205, 193);
                $peachpuff = ImageColorAllocate($img, 255, 218, 185);
                $mistyrose = ImageColorAllocate($img, 255, 228, 225);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);
                //$honeydew = ImageColorAllocate($img, 240, 255, 240);

                
                //$val =  sprintf("#%06X\n", mt_rand( 0, 0xFFFFFF ));
                //$val2 = ImageColorAllocate($img, $val);
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $val2 = ImageColorAllocate($img, $color1, $color2, $color3);
                
                
                ImageFillToBorder($img, 80, 112, $border, ImageColorAllocate($img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255))); // region1
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                ImageFillToBorder($img, 171, 150, $border, ImageColorAllocate($img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
                ImageFillToBorder($img, 204, 71, $border, ImageColorAllocate($img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
                ImageFillToBorder($img, 240, 190, $border, ImageColorAllocate($img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
                ImageFillToBorder($img, 250, 140, $border, ImageColorAllocate($img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);
                //ImageFillToBorder($img, 80, 112, $border, $red);

                // Output image as PNG
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
            } else {
                echo "<br>No image loaded.</br>";
            }
            
            /*
             * $filename="/path/to/file.jpg"; //<-- specify the image  file
                if(file_exists($filename)){ 
                header('Content-Length: '.filesize($filename])); //<-- sends filesize header
                header('Content-Type: image/jpg'); //<-- send mime-type header
                header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header
                readfile($filename); //<--reads and outputs the file onto the output buffer
                die(); //<--cleanup
                exit; //and exit
                }
             * 
             */
            
        } /// end public function maptest2()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function set_region_coords() {
            
            // ocean, 336, 437
            
            $allregions = array(
                "1" => array(104, 117),
                "2" => array(204, 80),
                "3" => array(258, 90),
                "4" => array(165, 137),
                "5" => array(239, 203),
                "6" => array(271, 146),
                "7" => array(120, 230),
                "8" => array(195, 235),
                "9" => array(109, 297),
                "10" => array(286, 264),
                "11" => array(306, 210),
                "12" => array(188, 288),
                "13" => array(257, 310),
                "14" => array(348, 188),
                "15" => array(343, 265),
                "16" => array(60, 451),
                "17" => array(76, 514),
                "18" => array(114, 424),
                "19" => array(134, 471),
                "20" => array(191, 472),
                "21" => array(296, 372),
                "22" => array(404, 238),
                "23" => array(461, 225),
                "24" => array(411, 292),
                "25" => array(463, 439),
                "26" => array(500, 292),
                "27" => array(570, 240),
                "28" => array(510, 178),
                "29" => array(520, 332),
                "30" => array(452, 486),
                "31" => array(395, 552),
                "32" => array(396, 591),
                "33" => array(495, 592),
                "34" => array(456, 550),
                "35" => array(545, 516),
                "36" => array(541, 593),
                "37" => array(591, 592),
                "38" => array(566, 470),
                "39" => array(542, 405),
                "40" => array(586, 372),
                "41" => array(621, 455),
                "42" => array(638, 533),
                "43" => array(654, 480),
                "44" => array(592, 176),
                "45" => array(564, 135),
                "46" => array(637, 243),
                "47" => array(626, 293),
                "48" => array(641, 351),
                "49" => array(688, 416),
                "50" => array(720, 453),
                "51" => array(689, 173),
                "52" => array(716, 59),
                "53" => array(735, 104),
                "54" => array(692, 276),
                "55" => array(730, 220),
                "56" => array(749, 287),
                "57" => array(786, 416),
                "58" => array(795, 497),
                "59" => array(834, 537),
                "60" => array(863, 396),
                "61" => array(888, 362),
                "62" => array(923, 414),
                "63" => array(863, 267),
                "64" => array(834, 226),
                "65" => array(863, 118),
                "66" => array(849, 40),
                "67" => array(1032, 116),
                "68" => array(924, 148),
                "69" => array(956, 200),
                "70" => array(905, 307),
                "71" => array(992, 387),
                "72" => array(947, 484),
                "73" => array(959, 536),
                "74" => array(1047, 546),
                "75" => array(1084, 567),
                "76" => array(1067, 485),
                "77" => array(1033, 408),
                "78" => array(1083, 389),
                "79" => array(1085, 454),
                "80" => array(1162, 568),
                "81" => array(1239, 519),
                "82" => array(1219, 475),
                "83" => array(1134, 478),
                "84" => array(1131, 399),
                "85" => array(1025, 220),
                "86" => array(1004, 253),
                "87" => array(1004, 293),
                "88" => array(1104, 309),
                "89" => array(1105, 211),
                "90" => array(1127, 108),
                "91" => array(1175, 113),
                "92" => array(1169, 180),
                "93" => array(1176, 242),
                "94" => array(1223, 105),
                "95" => array(1230, 189),
                "96" => array(1242, 258),
                "97" => array(789, 270),
                "98" => array(787, 314),
                "99" => array(1232, 306),
                "100" => array(782, 93),
                
                );
            
            //print_r($allregions);
            
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Regions u 
                        ");
            
            $regions = $query->getResult();
            
            $i = '1';
            foreach ($regions as $row) {
                
                
                //echo $allregions[$i]['0'];
                //print_r($allregions[$i]);
                //echo "<br>";
                
                $row->setCoordX($allregions[$i]['0']);
                $row->setCoordY($allregions[$i]['1']);
                $this->doctrine->em->persist($row);
                
                $i++;
                
                //$row->setMarriageCode(NULL);
                
            }
            $this->doctrine->em->flush(); 
        } /// end public function set_region_coords()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        public function create_region_neighbors() {
            
            
            
            $allregions = array(
                "1" => array(2, 4),
                "2" => array(1, 4, 6, 3),
                "3" => array(2, 6),
                "4" => array(1, 2, 6, 5, 8),
                "5" => array(4, 6, 11, 10, 8),
                "6" => array(4, 2, 3, 11, 5),
                "7" => array(9, 8, 12),
                "8" => array(7, 4, 5, 10, 12),
                "9" => array(7, 12, 16),
                "10" => array(8, 5, 11, 13, 12),
                "11" => array(6, 14, 15, 21, 13, 10, 5),
                "12" => array(9, 7, 8, 10, 13, 18, 16),
                "13" => array(12, 10, 11, 21),
                "14" => array(11, 15, 22),
                "15" => array(11, 14, 22, 21),
                "16" => array(9, 12, 18, 19, 17),
                "17" => array(16, 19, 20),
                "18" => array(16, 12, 19),
                "19" => array(20, 17, 16, 18),
                "20" => array(19, 17, 32),
                "21" => array(13, 11, 15, 24),
                "22" => array(15, 14, 23, 26, 24),
                "23" => array(22, 28, 27, 26),
                "24" => array(26, 29, 22),
                "25" => array(29, 38, 35, 30),
                "26" => array(22, 23, 27, 47, 40, 29, 24),
                "27" => array(23, 28, 44, 46, 47, 26),
                "28" => array(23, 45, 44, 46, 27),
                "29" => array(26, 24, 40, 39, 38, 25),
                "30" => array(31, 34, 35, 25),
                "31" => array(32, 33, 34, 30),
                "32" => array(20, 31, 33),
                "33" => array(32, 31, 34, 36),
                "34" => array(30, 31, 33, 36, 35),
                "35" => array(37, 42, 43, 41, 38, 25, 30, 34, 36),
                "36" => array(33, 37, 35, 34),
                "37" => array(36, 35, 42),
                "38" => array(25, 29, 39, 40, 41, 35),
                "39" => array(29, 40, 38),
                "40" => array(39, 29, 26, 47, 48, 41, 38),
                "41" => array(38, 40, 48, 49, 50, 43, 35),
                "42" => array(37, 35, 43),
                "43" => array(42, 35, 41, 50),
                "44" => array(45, 28, 27, 46, 51),
                "45" => array(28, 44),
                "46" => array(28, 44, 51, 54, 47, 27),
                "47" => array(27, 46, 54, 48, 40, 26),
                "48" => array(40, 47, 54, 56, 49, 41),
                "49" => array(48, 56, 57, 50, 41),
                "50" => array(43, 41, 49, 57, 58),
                "51" => array(44, 46, 54, 55, 53),
                "52" => array(53, 100, 66),
                "53" => array(51, 55, 64, 100, 52),
                "54" => array(47, 46, 51, 55, 97, 56, 48),
                "55" => array(51, 53, 64, 97, 54),
                "56" => array(54, 97, 98, 57, 49, 48),
                "57" => array(49, 56, 98, 60, 59, 58, 50),
                "58" => array(50, 57, 59),
                "59" => array(58, 57, 60, 62),
                "60" => array(47, 98, 61, 62, 59),
                "61" => array(60, 98, 97, 96, 70, 71, 62),
                "62" => array(59, 60, 61, 71, 74, 72),
                "63" => array(61, 97, 55, 64, 68, 69, 70),
                "64" => array(55, 53, 100, 65, 68, 63),
                "65" => array(100, 66, 67, 68, 64),
                "66" => array(52, 100, 65),
                "67" => array(65, 90, 89, 85, 69, 68),
                "68" => array(65, 67, 69, 63, 64),
                "69" => array(63, 68, 67, 85, 86, 70),
                "70" => array(63, 69, 86, 87, 71, 61),
                "71" => array(70, 87, 77, 76, 74, 62, 71),
                "72" => array(62, 74, 73),
                "73" => array(72, 74),
                "74" => array(72, 73, 62, 71, 76, 75),
                "75" => array(74, 76, 79, 80),
                "76" => array(74, 75, 79, 78),
                "77" => array(71, 76, 78, 84, 87, 88),
                "78" => array(77, 84, 83, 79, 76),
                "79" => array(76, 78, 83, 80, 75),
                "80" => array(75, 79, 83, 81),
                "81" => array(80, 82, 83),
                "82" => array(83, 81),
                "83" => array(80, 82, 83, 79, 78, 84),
                "84" => array(83, 78, 77, 88),
                "85" => array(69, 67, 89, 86),
                "86" => array(70, 69, 85, 89, 88, 87),
                "87" => array(86, 88, 77, 71, 70),
                "88" => array(93, 84, 77, 87, 86, 89),
                "89" => array(85, 87, 90, 92, 93, 88, 86),
                "90" => array(67, 91, 92, 89),
                "91" => array(90, 94, 92),
                "92" => array(94, 91, 90, 89, 95, 93),
                "93" => array(88, 89, 92, 95, 96),
                "94" => array(91, 92),
                "95" => array(92, 99, 96, 93),
                "96" => array(99, 95, 93),
                "97" => array(54, 55, 63, 61, 98, 56),
                "98" => array(97, 61, 60, 57, 56),
                "99" => array(96, 95),
                "100" => array(53, 52, 66, 65, 64),
                
                );
            
            
            $ci =& get_instance();
            //$query = $ci->doctrine->em->createQuery("select u from ORM\Dynasties2\Regions u");
            //$result = $query->getResult();
            $query = $ci->doctrine->em->createQuery("select min(u.id) from ORM\Dynasties2\Regions u");
            $result = $query->getSingleResult();
            $lowval = $result[1] - 1;
            
            //print_r($result);
            
            
            foreach ($allregions as $key => $row) {
                
                foreach ($row as $row2) {
                    
                    //echo $key;
                    //echo $lowval;
                    $val = $lowval + $row2;
                    $keyval = $lowval + $key;
                    
                    //echo "test, $key, " . $val . "<br>";
                    //echo $row2;
                    $new_neighbor = new ORM\Dynasties2\Regionneighbors;
                    $new_neighbor->setWhichRegion($keyval);
                    $new_neighbor->setNeighborRegion($val);
                    $this->doctrine->em->persist($new_neighbor);
                    
                }
                
            }
            $this->doctrine->em->flush(); 
            
           
            
            
            
        } /// end public function create_region_neighbors()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        
        public function create_regions() {
            
            $time_start = microtime(true);
            // create 100 regions
            $i = '1';
            
            while ($i < '101') {
                
                
                $region_data['world'] = '1';
                $region_data['name'] = 'region' . $i;
                $region_data['maxSoldiers'] = '500';
                $region_data['loyalty'] = '70';
                $region_data['infrastructure'] = '75';
                $region_data['taxincome'] = '50';
              
                $this->load->model('setup_data_model'); 
                $this->setup_data_model->create_regions($region_data); 
                //echo "done</br>";
                $i++;
            } // end while that runs 100 times
            
            
        $this->doctrine->em->flush();
        
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "<br>Regions created in:  " . round($time, 3) . " seconds\n";
        
        } /// end public function create_world()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function destroy_regions() {
            //$time_start = microtime(true);
            
            $this->load->model('setup_data_model'); 
            $this->setup_data_model->destroy_regions(); 
            
            //$time_end = microtime(true);
            //$time = $time_end - $time_start;
            //echo "<br>Load Time:  " . round($time, 3) . " seconds\n";
            
        } /// end public function destroy_world()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function create_characters() {
            $time_start = microtime(true);
            
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $current_turn = $turndata->getTurnNumber();
            
            $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Regions u
                ");
            $region = $query->getResult();

            
            $famcounter = '100';
            foreach ($region as $row) {
                $time_start2 = microtime(true);
                // create a character
                //$this->load->helper('character_helper');
                $name = $this->generate_male_name();
                //echo $name;
                
                $character_data['name'] = $name;
                //$character_data['deathturn'] = '0';
                
                $rand = mt_rand(1,10);
                if ($rand < '5') {
                    // he or she is young, no children, possibly married
                    $age_rand = mt_rand(136,192);
                    $born_turn = $current_turn - $age_rand;
                    $rand = mt_rand(1,2);
                    if ($rand == '1') {
                        // not married
                        $makespouse = '0';
                    } else {
                        // married
                        $makespouse = '1';
                    }
                    $makechildren = '0';
                    
                    $prestige_val = '10';
                } else if ($rand < '9') {
                    // he or she is middle aged, some young children perhaps, probably married
                    $age_rand = mt_rand(200,288);
                    $born_turn = $current_turn - $age_rand;
                    
                    $rand = mt_rand(1,5);
                    if ($rand == '1') {
                        // not married
                        $makespouse = '0';
                    } else {
                        // married
                        $makespouse = '1';
                    }
                    
                    // kids?
                    $rand = mt_rand(1,4);
                    if ($rand == '1') {
                        // no kids
                        $makechildren = '0';
                    } else {
                        // kids
                        $makechildren = '1';
                    }
                    
                    
                    $prestige_val = '50';
                } else {
                    // he or she is rather old, some grown children, probably married or widowed
                    $age_rand = mt_rand(320,400);
                    $born_turn = $current_turn - $age_rand;
                    
                    $rand = mt_rand(1,4);
                    if ($rand == '1') {
                        // not married
                        $makespouse = '0';
                    } else {
                        // married
                        $makespouse = '1';
                    }
                    // kids?
                    $rand = mt_rand(1,100);
                    if ($rand == '1') {
                        // no kids
                        $makechildren = '0';
                    } else {
                        // kids
                        $makechildren = '1';
                    }
                    
                    $prestige_val = '100';
                }  /// end the if else set to do code based on character age
                

                
                $character_data['bornturn'] = $born_turn;
                $character_data['isfemale'] = '0';
                $character_data['playercontrolled'] = '0';
                $character_data['useai'] = '1';
                $character_data['isruler'] = '1';
                $character_data['fathersID'] = '0';
                $character_data['mothersID'] = '0';
                $character_data['prestige'] = $prestige_val;
                $character_data['ambition'] = $this->tendten();
                $character_data['leadership'] = $this->tendten();
                $character_data['money'] = '100';
                $character_data['diplomacy'] =  $this->tendten();
                
                $character_data['marriedto'] = NULL;
                $character_data['marriedturn'] = NULL;
                
                $character_data['whichfamily'] = $famcounter;
                    
                
                
                
                $this->load->model('setup_data_model'); 
                //$this->setup_data_model->commit_new_character_to_db_v2($character_data); 
                
                $char_id = $this->setup_data_model->commit_new_character_to_db_v2($character_data); 
                //echo $char_id;
                
                if ($makespouse === '1') {
                    
                    
                    // determine spouse age
                    // $age_rand is the age of husband in turns.
                    $spouse_age_diff = mt_rand(-32,32);  // up to 4 years difference, either way.
                    $spouse_age_turns = $age_rand + $spouse_age_diff;
                    $spouse_born_turn = $current_turn - $spouse_age_turns;
                    
                    // determine marriage date
                    $turns_since_16 = $age_rand - 128;
                    $turns_since_24 = $age_rand - 192;
                    if ($turns_since_24 < '1') $turns_since_24 = '1';
                    $large_marriage_range = mt_rand(1, $turns_since_16);
                    $marriage_range = mt_rand ($turns_since_24, $turns_since_16);
                    $marriage_turn = $current_turn - $marriage_range;
                    
                                       
                    //echo "<br>Making a wife!</br>";
                    $spouse_data['name'] = $this->generate_female_name();
                    $spouse_data['bornturn'] = $spouse_born_turn;
                    $spouse_data['isfemale'] = '1';
                    $spouse_data['marriedto'] = $char_id;
                    $spouse_data['marriedturn'] = $marriage_turn;
                    $spouse_data['playercontrolled'] = '0';
                    $spouse_data['useai'] = '1';
                    $spouse_data['isruler'] = '0';
                    $spouse_data['fathersID'] = '0';
                    $spouse_data['mothersID'] = '0';
                    $spouse_data['prestige'] =  '0';
                    $spouse_data['ambition'] =  $this->tendten();
                    $spouse_data['leadership'] = $this->tendten();
                    $spouse_data['whichfamily'] = $famcounter;
                    $spouse_data['money'] = '0';
                    $spouse_data['diplomacy'] =  $this->tendten();
                    
                    $this->load->model('setup_data_model'); 
                    $spouse_id = $this->setup_data_model->commit_new_character_to_db_v2($spouse_data); 
                    
                    $update_husband = $this->doctrine->em->find('ORM\Dynasties2\Characters', $char_id);
                    
                    $update_husband->setMarriedDate($marriage_turn);
                    $update_husband->setMarriedTo($spouse_id);
                    $this->doctrine->em->persist($update_husband);
                    $this->doctrine->em->flush();  
                    
                } /// end spouse code
                
                $spouse_age_counter = '0';
                $numberofchildren = '0';
                if ($makechildren === '1' && $makespouse === '1') {
                    
                    // determine how long we've been able to make kids, age and married date
                    //$marriage_turn
                    //$spouse_age_turns
                    //$age_rand
                    // each pregnancy lasts 6 game turns.  concieve end of turn 1, pregs 2, 3, 4, 5 and give birth end of 6.
                    $turn_diff = $current_turn - $marriage_turn;
                    $spouse_age_counter = $spouse_age_turns - $turn_diff;
                    $turncounter = $marriage_turn;
                    while ($turncounter < ($current_turn - 6)) {
                        if ($spouse_age_counter < '201') {
                            
                            // chance to concieve
                            $concieve = mt_rand(1,5);
                            if ($concieve < '3') {
                                // concieved!
                                $turncounter = $turncounter + '7';
                                $lived = mt_rand(1,5);
                                if ($lived < '3') {
                                    // child survives
                                    $child_data['bornturn'] = $turncounter - '6';
                                    $sex = mt_rand(1,2);
                                    if ($sex < '2') {
                                        //echo "male";
                                        $gender = '0'; // male
                                        $child_data['name'] = $this->generate_male_name();
                                        $child_data['isfemale'] = '0';
                                    } else {
                                        $gender = '1'; // female
                                        $child_data['name'] = $this->generate_female_name();
                                        $child_data['isfemale'] = '1';
                                    } /// end gender if else
                                    
                                    $numberofchildren++;
                                    
                                    $child_data['marriedto'] = NULL;
                                    $child_data['marriedturn'] = NULL;
                                    $child_data['playercontrolled'] = '0';
                                    $child_data['useai'] = '1';
                                    $child_data['isruler'] = '0';
                                    $child_data['fathersID'] = $char_id;
                                    $child_data['mothersID'] = $spouse_id;
                                    $child_data['prestige'] =  '0';
                                    $child_data['ambition'] =  $this->tendten();
                                    $child_data['diplomacy'] =  $this->tendten();
                                    $child_data['leadership'] = $this->tendten();
                                    $child_data['whichfamily'] = $famcounter;
                                    $child_data['money'] = '0';
                                    
                                    //echo "<br>A living child, [$numberofchildren, $turncounter]</br>";
                                    $child_id = $this->setup_data_model->commit_new_character_to_db_v2($child_data); 
                                    
                                    
                                }  // end chance for child to live
                                
                            } // end chance to concieve
                            
                        } /// end age check, women over 40 cannot concieve
                        
                        $spouse_age_counter++;
                        $turncounter++;
                    }
                    
                    
                    
                }   /// end make children code
                
                $time_end = microtime(true);
                $time = $time_end - $time_start2;
                //echo "Region populated in:  " . round($time, 3) . " seconds, ";
                $famcounter--;
            } /// end foreach region as row
            
        $this->doctrine->em->flush();
        
        
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "<br>All characters created in:  " . round($time, 3) . " seconds\n";
            
        } /// end public function create_characters()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function destroy_characters() {
            
            //$time_start = microtime(true);
            
            $this->load->model('setup_data_model'); 
            $this->setup_data_model->destroy_characters(); 
            
            //$time_end = microtime(true);
            //$time = $time_end - $time_start;
            //echo "<br>Load Time:  " . round($time, 3) . " seconds\n";
            
        } /// end public function destroy_characters()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function create_titles() {
            $time_start = microtime(true);
            
            $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Characters u
                WHERE u.isRuler = '1'");
            $result1 = $query->getResult();
            
            $ci =& get_instance();
            $query = $ci->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Regions u");
            $result = $query->getSingleResult();
            $highval = $result[1];
            $query = $ci->doctrine->em->createQuery("select min(u.id) from ORM\Dynasties2\Regions u");
            $result = $query->getSingleResult();
            $lowval = $result[1];
            
            foreach ($result1 as $row) {
                //echo $row->getName();
                
                $data['charid'] = $row->getId();
                $data['region'] = $lowval;
                $lowval++;
                
                $this->load->model('setup_data_model'); 
                $char_id = $this->setup_data_model->create_titles($data); 
                
                
            }
            
            
        $this->doctrine->em->flush();
        
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "<br>Titles created in:  " . round($time, 3) . " seconds\n";
            
        } /// end public function create_titles()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_titles() {
        
       
             $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Titles u
                ");
             $result1 = $query->getResult();
            
             foreach ($result1 as $row) {
                 
                 if ($row->getTitleType() == '1') $titlename = 'Count';
                 //echo $row->getId();
                 //$character = $this->doctrine->em->find('ORM\Dynasties2\Characters', $row->getWhichCharacter());
                 $characterdata = $row->getCharacterLink();
                 
                 
                 $family = $this->doctrine->em->find('ORM\Dynasties2\Families', $characterdata->getWhichFamily());
                 
                 //$region = $this->doctrine->em->find('ORM\Dynasties2\Regions', $row->getWhichRegion());
                 $regiondata = $row->getRegionLink();
                 
                 //$user = $article->getUser();
                 
                 //echo $character->getName();
                 echo $characterdata->getCharacterName() . " " . $family->getFamilyName() . ", " . $titlename . " of " . $regiondata->getRegionName() . "."; 
                 echo "</br>";
             }
        

        
        
    }   ////// end of public function display_titles
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function destroy_titles() {
            
            //$time_start = microtime(true);
            
            $this->load->model('setup_data_model'); 
            $this->setup_data_model->destroy_titles(); 
            
            //$time_end = microtime(true);
            //$time = $time_end - $time_start;
            //echo "<br>Load Time:  " . round($time, 3) . " seconds\n";
            
        } /// end public function destroy_titles()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function create_family_names() {
            
            $names[] = "Lannister";
            $names[] = "Stark";
            $names[] = "Tully";
            $names[] = "Baratheon";
            $names[] = "von Hapsburg";
            $names[] = "Saxe-Coburg and Gotha";
            $names[] = "Piast";
            $names[] = "Romanov";
            $names[] = "Hannover";
            $names[] = "Oldenburg";
            $names[] = "Bourbon";
            $names[] = "Arpad";
            $names[] = "O'Brien";
            $names[] = "Hesse";
            $names[] = "York";
            $names[] = "Tudor";
            $names[] = "Stuart";
            $names[] = "Pahlavi";
            $names[] = "Savoy";
            $names[] = "Lancaster";
            $names[] = "Premyslid";
            $names[] = "Holstein";
            $names[] = "Capet";
            $names[] = "Orleans";
            $names[] = "Flavia";
            $names[] = "Nemanjic";
            $names[] = "Grimaldi";
            $names[] = "Blackmont";
            $names[] = "Grimm";
            $names[] = "Hewett";
            $names[] = "Webber";
            $names[] = "Westerling";
            $names[] = "Glover";
            $names[] = "Chester";
            $names[] = "Coldwater";
            $names[] = "Errol";
            $names[] = "Felwood";
            $names[] = "Gaunt";
            $names[] = "Prester";
            $names[] = "Plumm";
            $names[] = "Moore";
            $names[] = "Haigh";
            $names[] = "Sharp";
            $names[] = "Spicer";
            $names[] = "Stout";
            $names[] = "Swyft";
            $names[] = "Vypren";
            $names[] = "Saltcliffe";
            $names[] = "Roosby";
            $names[] = "Gordon";
            $names[] = "Rye";
            $names[] = "Bartly";
            $names[] = "Ashford";
            $names[] = "Cole";
            $names[] = "Condon";
            $names[] = "Connagh";
            $names[] = "Ashlyn";
            $names[] = "Gower";
            $names[] = "Goodbrooke";
            $names[] = "Wynch";
            $names[] = "Drumm";
            $names[] = "Hastwyck";
            $names[] = "Broom";
            $names[] = "Boggs";
            $names[] = "Foote";
            $names[] = "Flint";
            $names[] = "Inchfield";
            $names[] = "Ironsmith";
            $names[] = "Jast";
            $names[] = "Keeling";
            $names[] = "Kellington";
            $names[] = "Hetchen";
            $names[] = "Glayne";
            $names[] = "Mooresbrook";
            $names[] = "Norcross";
            $names[] = "Lundwyst";
            $names[] = "Page";
            $names[] = "Reyne";
            $names[] = "Farwynd";
            $names[] = "Vasa";
            $names[] = "Liddle";
            $names[] = "Enburg";
            $names[] = "Fennel";
            $names[] = "Garrenal";
            $names[] = "Illent";
            $names[] = "Jaaven";
            $names[] = "Olsted";
            $names[] = "Pyrre";
            $names[] = "Quence";
            $names[] = "Stavec";
            $names[] = "Tenwood";
            $names[] = "Ulvid";
            $names[] = "Vangel";
            $names[] = "Wymouth";
            $names[] = "Yaven";
            $names[] = "Aachen";
            $names[] = "Borrell";
            $names[] = "Myre";
            $names[] = "Norris";
            $names[] = "Toyne";
            
            //print_r($names);
            
            $this->load->model('setup_data_model'); 
            $this->setup_data_model->create_families($names);
            
            $this->doctrine->em->flush();
            
        } /// end public function create_family_names()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        public function generate_male_name() {
            $ci =& get_instance();

            $query = $ci->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Malenames u");
            $result = $query->getSingleResult();
            $highval = $result[1];
            $random_name = rand(1,$highval);
            $name = $ci->doctrine->em->find('ORM\Dynasties2\Malenames', $random_name);
            return $name->getName();


        } /// end public function generate_male_name()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function generate_female_name() {
            $ci =& get_instance();
            
            $query = $ci->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Femalenames u");
            $result = $query->getSingleResult();
            $highval = $result[1];
            $random_name = rand(1,$highval);
            $name = $ci->doctrine->em->find('ORM\Dynasties2\Femalenames', $random_name);
            return $name->getName();
                    
        }/// end public function generate_female_name()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function tendten() {
            
            $number = '10';
            $results = '0';
            while ($number > '0') {
                $dice = mt_rand(1,10);
                //echo $dice . "</br>";
                $results = $dice + $results;
                  
                $number--;
            }
            //echo "<br>* $results * </br>";
            return $results;
                    
        }/// end public function tendten()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function fivedtwenty() {
            $number = '5';
            $results = '0';
            while ($number > '0') {
                $dice = mt_rand(1,20);
                //echo $dice . "</br>";
                $results = $dice + $results;
                  
                $number--;
            }
            //echo "<br>* $results * </br>";
            return $results;
            
            
                    
        }/// end public function tendten()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        public function display_gamedata() {   
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $current_turn = $turndata->getTurnNumber();      
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Families u");
            $famresult = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Characters u");
            $charresult = $query->getResult();
            
            foreach ($famresult as $row) {
                //print_r($row);
                echo "<br><b>" . $row-> getFamilyName() . "</b>: ";
                $val = $row-> getId();
                
                $query = $this->doctrine->em->createQuery("SELECT u from ORM\Dynasties2\Characters u WHERE u.whichFamily = $val");
                $charresult_narrow = $query->getResult();
                
                foreach ($charresult_narrow as $row2) {
                    //print_r($row);
                    
                    $years_old = ($current_turn - $row2->getBornDate()) / 8;
                    echo $row2->getCharacterName() . " ($years_old), ";
                }
                
            }
            
            
            
        }/// end public function display_gamedata()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function display_nobility() {   
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $current_turn = $turndata->getTurnNumber();      
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Families u");
            $fam_result = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Titles u");
            $titles_result = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Claims u");
            $claims_result = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Characters u");
            $char_result = $query->getResult();
            
            foreach ($fam_result as $row) {
                //print_r($row);
                echo "<br><b>" . $row-> getFamilyName() . "</b>: ";
                $val = $row-> getId();
                
                $query = $this->doctrine->em->createQuery("SELECT u from ORM\Dynasties2\Characters u WHERE u.whichFamily = $val");
                $charresult_narrow = $query->getResult();
                
                foreach ($charresult_narrow as $row2) {
                    //print_r($row);
                    if ($row2->getIsRuler() == '1') {
                        $years_old = ($current_turn - $row2->getBornDate()) / 8;
                        echo $row2->getCharacterName() . " (prestige: " . $row2->getPrestige() . ")  ";

                        $char_id = $row2->getId();
                        $query = $this->doctrine->em->createQuery("SELECT u from ORM\Dynasties2\Titles u WHERE u.characterLink = $char_id");
                        $my_titles = $query->getResult();
                        foreach ($my_titles as $row3) {
                            echo "Count of " . $row3->getRegionLink()->getRegionName() . ", ";
                        }
                    }
                    
                    
                }
                
            }
            
            
            
        }/// end public function display_nobility()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        
        public function display_gamelogs() {
             $ci =& get_instance();
             
            $query = $ci->doctrine->em->createQuery("select u from ORM\Dynasties2\Gamelog u");
            $logdata = $query->getResult();
            
            foreach ($logdata as $row) {
                echo $row->getOnTurn() . " - " . $row->getText() . "</br>";
            }
            
        } /// end public function display_gamelogs()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function reset_gamedata() {
            
            
            echo "<br>Deleting Betrothals...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Betrothals u
                    ");
            $region = $query->getResult();

            foreach ($region as $row) {
                $this->doctrine->em->remove($row);
            }
            echo "OK</br>";
            
            echo "<br>Deleting Wars...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Wars u
                    ");
            $region = $query->getResult();

            foreach ($region as $row) {
                $this->doctrine->em->remove($row);
            }
            echo "OK</br>";
            
            echo "<br>Deleting Armies...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Armies u
                    ");
            $region = $query->getResult();

            foreach ($region as $row) {
                $this->doctrine->em->remove($row);
            }
            echo "OK</br>";
            
            echo "<br>Deleting Army Forces...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Armyforces u
                    ");
            $region = $query->getResult();

            foreach ($region as $row) {
                $this->doctrine->em->remove($row);
            }
            echo "OK</br>";
            
            echo "<br>Deleting Claims...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Claims u
                    ");
            $region = $query->getResult();

            foreach ($region as $row) {
                $this->doctrine->em->remove($row);
            }
            
            echo "OK</br>";
            
            
            

            $this->doctrine->em->flush();
            
            
        } /// end public function reset_gamedata()
////////////////////////////////////////////////////////////////////////////////////////////////

        public function destroy_all() {
            
            $logmsg = new ORM\Dynasties2\Systemlog;
            $logmsg -> setText("DESTROY ALL function was called");
            $logmsg -> setTimeStamp(new DateTime());
            $logmsg -> setMsgType('2');
            $logmsg -> setipAddress($this->input->ip_address());
            $this->doctrine->em->persist($logmsg);
            $this->doctrine->em->flush();
            
            $time_start = microtime(true);
            
            //
            // reset current turn to 0
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $turndata->setTurnNumber('0');
            
            
            //
            // destroy war participants
            $time_start_local = microtime(true);
            echo "<br>Deleting War participants...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Warparticipants u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            //
            // destroy army forces
            $time_start_local = microtime(true);
            echo "<br>Deleting Army Forces...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Armyforces u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
             //
            // destroy armies
            $time_start_local = microtime(true);
            echo "<br>Deleting Armies...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Armies u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            
            
            //
            // destroy wars
            $time_start_local = microtime(true);
            echo "<br>Deleting Wars...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Wars u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            //
            // destroy claims
            $time_start_local = microtime(true);
            echo "<br>Deleting Claims...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Claims u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            //
            // destroy duchy regions
            $time_start_local = microtime(true);
            echo "<br>Deleting Duchy Regions...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Duchyregions u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            //
            // destroy Proposals
            $time_start_local = microtime(true);
            echo "<br>Deleting Proposals...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Proposals u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            
            //
            // destroy duchies
            $time_start_local = microtime(true);
            echo "<br>Deleting Duchies...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Duchies u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
           
            
            //
            // destroy titles
            $time_start_local = microtime(true);
            echo "<br>Deleting Titles...... ";
            $this->destroy_titles();
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            //
            // destroy characters
            $time_start_local = microtime(true);
            echo "<br>Deleting Characters...... ";
            $this->destroy_characters();
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            
            
            
            //
            // destroy regions
            $time_start_local = microtime(true);
            echo "<br>Deleting Regions...... ";
            $this->destroy_regions();
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
           
            
            //
            // destroy betrothals
            $time_start_local = microtime(true);
            echo "<br>Deleting Betrothals...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Betrothals u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            
            //
            // destroy game log
            $time_start_local = microtime(true);
            echo "<br>Deleting Game Log...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Gamelog u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            //
            // destroy region neighbors?
            $time_start_local = microtime(true);
            echo "<br>Deleting Region Neighbors...... ";
            $query = $this->doctrine->em->createQuery("SELECT u 
                    FROM ORM\Dynasties2\Regionneighbors u
                    ");
            $data = $query->getResult();
            foreach ($data as $row) {
                $this->doctrine->em->remove($row);
            }
            $time_end_local = microtime(true);
            $time = $time_end_local - $time_start_local;
            echo "OK, took " . round($time, 3) . " seconds</br>";
            
            
            $this->doctrine->em->flush(); 
            
            ///////////////////////
            $time_end = microtime(true);
            $time = $time_end - $time_start;
            echo "** Total, took " . round($time, 3) . " seconds</br>";
            
            
        } /// end public function destroy_all()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function setup_all() {
            
            $time_start = microtime(true);
            
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $current_turn = $turndata->getTurnNumber();
            
            if ($current_turn == '0') {
                
                $logmsg = new ORM\Dynasties2\Systemlog;
                $logmsg -> setText("SETUP ALL function was called");
                $logmsg -> setTimeStamp(new DateTime());
                $logmsg -> setMsgType('2');
                $logmsg -> setipAddress($this->input->ip_address());
                $this->doctrine->em->persist($logmsg);
                $this->doctrine->em->flush();
                
                
                // create characters
                $time_start_local = microtime(true);
                echo "<br>Creating Characters...... ";
                $this->create_characters();
                $time_end_local = microtime(true);
                $time = $time_end_local - $time_start_local;
                echo "OK, took " . round($time, 3) . " seconds</br>";
                
                // create regions
                $time_start_local = microtime(true);
                echo "<br>Creating Regions...... ";
                $this->create_regions();
                $time_end_local = microtime(true);
                $time = $time_end_local - $time_start_local;
                echo "OK, took " . round($time, 3) . " seconds</br>";
                
                // create region neighbors
                $time_start_local = microtime(true);
                echo "<br>Creating Region Neighbors...... ";
                $this->create_region_neighbors();
                $time_end_local = microtime(true);
                $time = $time_end_local - $time_start_local;
                echo "OK, took " . round($time, 3) . " seconds</br>";
                
                // create titles
                $time_start_local = microtime(true);
                echo "<br>Creating Titles...... ";
                $this->create_titles();
                $time_end_local = microtime(true);
                $time = $time_end_local - $time_start_local;
                echo "OK, took " . round($time, 3) . " seconds</br>";
                
                ///////////////////////
                $time_end = microtime(true);
                $time = $time_end - $time_start;
                echo "** Total, took " . round($time, 3) . " seconds</br>";
                
                
            } /// end if to make sure its turn 0
            /// no code beneath this line
            else {
                echo "<br>To create, you must first destroy.</br>";
            }
            
            
        } /// end public function setup_all()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        } /// end class SETUP
        
} //////// end session security check



